package com.kede.app.service.impl;

import com.kede.app.domain.OperatUser;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.OperatUserRepository;
import com.kede.app.service.OperatUserService;
import com.kede.app.service.dto.OperatUserDto;
import com.kede.app.service.dto.OperatUserQueryCriteria;
import com.kede.app.service.mapstruct.OperatUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-08-19
**/
@Service
@RequiredArgsConstructor
public class OperatUserServiceImpl implements OperatUserService {

    private final OperatUserRepository operatUserRepository;
    private final OperatUserMapper operatUserMapper;

    @Override
    public Map<String,Object> queryAll(OperatUserQueryCriteria criteria, Pageable pageable){
        Page<OperatUser> page = operatUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(operatUserMapper::toDto));
    }

    @Override
    public List<OperatUserDto> queryAll(OperatUserQueryCriteria criteria){
        return operatUserMapper.toDto(operatUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public OperatUserDto findById(String senceNo) {
        OperatUser operatUser = operatUserRepository.findById(senceNo).orElseGet(OperatUser::new);
        ValidationUtil.isNull(operatUser.getSenceNo(),"OperatUser","senceNo",senceNo);
        return operatUserMapper.toDto(operatUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OperatUserDto create(OperatUser resources) {
        return operatUserMapper.toDto(operatUserRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OperatUser resources) {
        OperatUser operatUser = operatUserRepository.findById(resources.getSenceNo()).orElseGet(OperatUser::new);
        ValidationUtil.isNull( operatUser.getSenceNo(),"OperatUser","id",resources.getSenceNo());
        operatUser.copy(resources);
        operatUserRepository.save(operatUser);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String senceNo : ids) {
            operatUserRepository.deleteById(senceNo);
        }
    }

    @Override
    public void download(List<OperatUserDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OperatUserDto operatUser : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" userPhone",  operatUser.getUserPhone());
            map.put(" adcode",  operatUser.getAdcode());
            map.put(" createTime",  operatUser.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}